﻿using EmployeeCard.Api.TestInterface;
using EmployeeCard.Extensions.Authorizations.Helpers;
using EmployeeCard.Model.ViewModels;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EmployeeCard.Api.Controllers
{
    /// <summary>
    /// 测试接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class HomeController : ControllerBase
    {
        private ISingTest _singTest;ITranTest _tranTest;ISconTest _sconTest;IAService _aService;
        public HomeController(ISingTest singTest,ITranTest tranTest,ISconTest sconTest,IAService aService)
        {
            _singTest = singTest;
            _tranTest = tranTest;
            _sconTest = sconTest;
            _aService = aService;
        }

        [HttpGet]
        public ActionResult<IEnumerable<string>> TestMethod()
        {
            _singTest.Name = "单例";
            _singTest.Age = 99;

            _tranTest.Name = "瞬态";
            _tranTest.Age = 1;

            _sconTest.Name = "作用域";
            _sconTest.Age = 100;

            _aService.RedisTest();

            return new string[] { "111", "222" };
        }

        /// <summary>
        /// 获取用户令牌
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetJwtStr(string name, string pass)
        {
            string jwtStr = string.Empty;
            bool success = false;

            var userRole = "Admin";//实际再数据库中获取用户角色
            if (userRole != null)
            {
                TokenInfoViewModel tokenModelJwt = new TokenInfoViewModel() { Uid = 1, Sub = userRole };
                jwtStr = JwtHelper.IssueJwt(tokenModelJwt);
                success = true;
            }
            else
            {
                jwtStr = "login fail!!!";
            }
            return Ok(new
            {
                success = success,
                token = jwtStr
            });
        }

        /// <summary>
        /// 管理员获取数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Policy = "Admin")]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}
